#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void seahag_test()
{
  int i, k, j, pass, numPlayers, coin_bonus, *topCard;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;
    // either 2, 3, or 4 players
    numPlayers = (rand() % 3) + 2;
    topCard = malloc(sizeof(int) * numPlayers);
   
    init_test_data(&state, numPlayers);
    
    srand(time(NULL));
    
    for(k = 0; k < numPlayers; k++){
      // set deck
      state.deckCount[k] = 50;
      for(j = 0; j < state.deckCount[k]; j++){
	state.deck[k][j] = random_card();
      }
      topCard[k] = state.deck[k][state.deckCount[k]];

      // set hand
      state.handCount[k] = 5;
      for(j = 0; j < state.handCount[k]; j++){
	state.hand[k][j] = random_card();
	if(j == 4)
	  topCard[k] = state.deck[k][j];
      }
    }

    printf("ITERATION %d\n", i);

    if(cardEffect(state.hand[0][4], -1, -1, -1, &state, 4, &coin_bonus) != 0)
      printf("Failure, sea hag effect not returned\n");
    else
      pass++;
    
    if(state.deck[0][state.deckCount[0]] == topCard[0])
      pass++;
    else
      printf("Failure, player 1 discarded the top card\n");
    
    for(j = 1; j < numPlayers; j++){
      if(state.deck[j][state.deckCount[j]] == topCard[j])
	printf("Failure, player %d did not discard top card\n", j + 1);
      else
	pass++;
      if(state.deck[j][state.deckCount[j]] != curse)
	printf("Failure, player %d did not gain a curse card on top of deck\n", j + 1);
      else
	pass++;  
    }
	  
    if(pass == (2 + (2 * numPlayers)))
      printf("Success, all tests passed\n");

  }
}

int main()
{
  seahag_test();
  return 0;
}
